package com.boss.thirteen.service.impl;

import com.boss.salary.dao.IAgencyInfoDao;
import com.boss.salary.dao.ISalaryBasedataDao;
import com.boss.salary.entity.AgencyInfo;
import com.boss.salary.entity.SalaryBasedata;
import com.boss.salary.service.IRedisService;
import com.boss.salary.vo.AgencySalaryVO;
import com.boss.sys.ca.user.dto.UserDTO;
import com.boss.thirteen.dao.ISalaryThirteenMonthDao;
import com.boss.thirteen.service.ISalaryThirteenMonthEndService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedInputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ThirteenSalaryExcelServiceImpl {

    @Autowired
    private IAgencyInfoDao iAgencyInfoDao;
    @Autowired
    private ISalaryThirteenMonthEndService salaryThirteenMonthEndService;
    @Autowired
    private ISalaryBasedataDao salaryBasedataDao;
    @Autowired
    private ISalaryThirteenMonthDao salaryThirteenMonthDao;
    @Autowired
    private IRedisService redisService;
    @Autowired
    private ThirteenSalaryExcelAsyn thirteenSalaryExcelAsyn;



    public void writeExcel03(String agencyId,UserDTO userDTO, OutputStream outputStream) throws Exception{
        AgencyInfo agencyInfo = iAgencyInfoDao.getNameById(agencyId);
        List<String> columnNameList = salaryThirteenMonthEndService.getColumnName(agencyId,new HashMap<>(),userDTO,false);
        List<SalaryBasedata> sb=salaryBasedataDao.selectAllSalary(userDTO.getProvince());
        Map<String ,String > salaryNameMap=sb.stream().collect(Collectors.toMap(SalaryBasedata::getSalaryCode, SalaryBasedata::getSalaryName));
        List<Map<String,Object>> unitSalaryReport=salaryThirteenMonthDao.selecctSalaryThirteenMonth(agencyId);
        //1,创建一个工作簿
        Workbook workbook = new HSSFWorkbook();
        //Workbook workbook = new XSSFWorkbook();
        //2,创建一个工作表
        Sheet sheet = workbook.createSheet("sheet1");
        //3，创建一行（1，1）
        Row row1 = sheet.createRow(0);
        //4，创建一个单元格
        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("单位");
        Cell cell12 = row1.createCell(1);
        cell12.setCellValue("单位编码");
        Cell cell14 = row1.createCell(2);
        cell14.setCellValue("身份证号");
        Cell cell15 = row1.createCell(3);
        cell15.setCellValue("姓名");
        int a=4;
        for (int i = 0; i < columnNameList.size(); i++) {
            row1.createCell(a).setCellValue(salaryNameMap.get(columnNameList.get(i)));
            a++;
        }

        for (int i = 0; i < unitSalaryReport.size(); i++) {
            Row row = sheet.createRow(i+1);

            row.createCell(0).setCellValue(agencyInfo.getAgencyName());
            row.createCell(1).setCellValue(agencyInfo.getAgencyCode());
            row.createCell(2).setCellValue(String.valueOf(unitSalaryReport.get(i).get("iden_no")));
            row.createCell(3).setCellValue(String.valueOf(unitSalaryReport.get(i).get("person_name")));
            for(int j=0; j<columnNameList.size(); j++){
                row.createCell(j+4).setCellValue(Double.parseDouble(String.valueOf(unitSalaryReport.get(i).get(columnNameList.get(j)))));
            }
        }

        //输出
        workbook.write(outputStream);
        //关闭流
        outputStream.close();

    }

    @Transactional(rollbackFor = Exception.class)
    public Map<String,String> ReadAsync(MultipartFile file, String province, String agencyId, List<AgencySalaryVO> exccelColumnName)  {
        Map<String,String>  remap=new HashMap<>();
        Object re = redisService.get(province+agencyId);
        if (re != null) {
            remap.put("Executing",province+agencyId);
            return remap;
        }
        try {
            //获取文件流
            BufferedInputStream fis = new BufferedInputStream(file.getInputStream());
            //获取文件名
            String fileName= file.getOriginalFilename();
            thirteenSalaryExcelAsyn.ReadAsync(fis,fileName,province,agencyId,exccelColumnName);
        } catch (Exception exception) {
            redisService.put(province+agencyId,"F-"+exception.getMessage());
            exception.printStackTrace();
        }

        redisService.put(province+agencyId,0);

        remap.put("guid",province+agencyId);

        return remap;
    }
}
